package com.sumainfor.pc.epc.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.epc.service.impl.KeepRepairService;
import com.sumainfor.pc.epc.service.impl.RepairServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 管家维修工单
 * @Auther: zhlu
 * @Date: 2019/3/5 16:04
 * @Description:
 */
@RestController
@RequestMapping("/repair")
public class KeepRepairController {

    Logger log= LoggerFactory.getLogger(KeepRepairController.class);

    @Autowired
    KeepRepairService KeepRepairService;

    @Autowired
    RepairServiceImpl RepairServiceImpl;


    /**
     * 功能描述: 
     * 获取维修工单列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/5 16:06
     */
    @ResponseBody
    @RequestMapping(value = "/getRepAirList",method = RequestMethod.GET)
    public JsonResult getRepAirList(@RequestParam Map<String,Object> params, Pager pager){
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        Integer RepairStatus=Integer.valueOf(params.get("RepairStatus").toString());
        if(RepairStatus==1||RepairStatus==2){
            if(RepairStatus==1){
                params.put("RepState",1);
            }
            else if(RepairStatus==2){
                params.put("RepState",2);
            }
        }else if(RepairStatus==3||RepairStatus==4){
            if(RepairStatus==3){
                params.put("OrdState",1);
            }
            else if(RepairStatus==4){
                params.put("OrdState",2);
            }
        }
        Integer RepairPatrol=Integer.valueOf(params.get("RepairPatrol").toString());
        if(RepairPatrol!=0){
            params.put("IfPatrol",params.get("RepairPatrol"));
        }
        log.info("params------"+params);
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=KeepRepairService.getRepAirListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getRepAirList=KeepRepairService.getRepAirList(params);
        result=pageUtils.getJsonResult(getRepAirList, params, cout);
        return result;
    }

    /**
     * 功能描述: 
     * 管家发起维修申请
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/26 11:54
     */
    @PostMapping("/AddRepair")
    public JsonResult AddRepair(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(KeepRepairService.AddRepair(params)){
            result.putSuccess("报修成功，请及时留意维修进度！");
        }else{
            result.putFailed("服务器繁忙,请稍后重试！");
        }
        return result;
    }
    
    /**
     * 功能描述: 
     * 工程助理强派工单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 18:59
     */
    @PostMapping("/AddRepairs")
    public JsonResult AddRepairs(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(RepairServiceImpl.AddRepair(params)){
            result.putSuccess("强派成功，请及时留意维修进度！");
        }else{
            result.putFailed("服务器繁忙,请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述: 
     * 根据维修工单编号获取维修资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/10 14:57
     */
    @GetMapping("/getRepairMap")
    public JsonResult getRepairMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"RepId"})){
            result.putFailed("RepId不能为空！");
            return  result;
        }
        result.put(RepairServiceImpl.getRepairMap(params));
        return result;
    }

}
